package com.example.mic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.example.voicedet.R;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

/**
 * 
 * @author tr
 * @time 2014-2-17
 * @description 悬浮窗管理类
 */
public class BigWindow {

/** 大悬浮窗view的实例 */
	public static FloatWindowBigView bigWindow;
/** 大悬浮窗view的参数 */
	private static LayoutParams bigWindowParams;
/** 用于控制在屏幕上添加或移除悬浮窗 */
	private static WindowManager mWindowManager;




/*** 创建一个大悬浮窗。初始位置在屏幕的右部中间位置 * 
* @param context*            必须为应用程序的Context	 */
	public static void createBigWindow(Context context,double signalEMA) {
		
		Log.d("Window_Set","Creat_Window   "+context.toString());
		WindowManager windowManager = getWindowManager(context);
		int screenWidth = windowManager.getDefaultDisplay().getWidth();
		int screenHeight = windowManager.getDefaultDisplay().getHeight();

		if (bigWindow == null) {
			bigWindow = new FloatWindowBigView(context,signalEMA);
			if (bigWindowParams == null) {
				bigWindowParams = new LayoutParams();
				bigWindowParams.x = screenWidth / 2
						- FloatWindowBigView.viewWidth / 2;
				bigWindowParams.y = screenHeight / 2
						- FloatWindowBigView.viewHeight / 2;
				bigWindowParams.type = LayoutParams.TYPE_PHONE;
				bigWindowParams.format = PixelFormat.RGBA_8888;
				bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
				bigWindowParams.width = FloatWindowBigView.viewWidth;
				bigWindowParams.height = FloatWindowBigView.viewHeight;
			}
		}
		windowManager.addView(bigWindow, bigWindowParams);

	}

/*** 将大悬浮窗从屏幕上移除 * 
* @param context     必须为应用程序的context*/
	public static void removeBigWindow(Context context) {
		if (bigWindow != null) {
			WindowManager windowManager = getWindowManager(context);
			windowManager.removeView(bigWindow);
			bigWindow = null;
		}

	}
	
	public static void updateBigWindow(double signalEMA) {
		//WindowManager windowManager = getWindowManager(context);
		if (bigWindow != null) {
			bigWindow.updateDisplay(signalEMA);
		}
		
	}


/*** 是否有悬浮窗(包括小悬浮窗、大悬浮窗和发射成功view)显示在屏幕上 * 
* @return 有悬浮窗显示在屏幕上返回true，没有的话返回false */
	public static boolean isWindowShowing() {
		return bigWindow != null;
	}

/*** 如果windowmanager还未创建，则创建一个新的windowmanager返回。否则返回当前以创建的windowmanager	 * 
* @param context           必须为应用程序的context
* @return windowmanager的实例，用于控制屏幕上添加或移除悬浮窗	 */
	private static WindowManager getWindowManager(Context context) {
		if (mWindowManager == null) {
			mWindowManager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);
		}
		return mWindowManager;
	}

}
